
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.eos &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.eos</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.eos</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module implements various equation of states.</span>

<span class="sd">Note: Most of the code were initially adapted from ASE and deltafactor by</span>
<span class="sd">@gmatteo but has since undergone major refactoring.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">deepcopy</span>
<span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABCMeta</span><span class="p">,</span> <span class="n">abstractmethod</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy.optimize</span> <span class="kn">import</span> <span class="n">leastsq</span><span class="p">,</span> <span class="n">minimize</span>

<span class="kn">from</span> <span class="nn">pymatgen.core.units</span> <span class="kn">import</span> <span class="n">FloatWithUnit</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.plotting</span> <span class="kn">import</span> <span class="n">pretty_plot</span><span class="p">,</span> <span class="n">add_fig_kwargs</span><span class="p">,</span> <span class="n">get_ax_fig_plt</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Kiran Mathew, gmatteo&quot;</span>
<span class="n">__credits__</span> <span class="o">=</span> <span class="s2">&quot;Cormac Toher&quot;</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span>


<div class="viewcode-block" id="EOSBase"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSBase">[docs]</a><span class="k">class</span> <span class="nc">EOSBase</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Abstract class that must be subcalssed by all equation of state</span>
<span class="sd">    implementations.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volumes</span><span class="p">,</span> <span class="n">energies</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">         Args:</span>
<span class="sd">             volumes (list/numpy.array): volumes in Ang^3</span>
<span class="sd">             energies (list/numpy.array): energy in eV</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">volumes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">volumes</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">energies</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">energies</span><span class="p">)</span>
        <span class="c1"># minimum energy(e0), buk modulus(b0),</span>
        <span class="c1"># derivative of bulk modulus wrt pressure(b1), minimum volume(v0)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># the eos function parameters. It is the same as _params except for</span>
        <span class="c1"># equation of states that uses polynomial fits(deltafactor and</span>
        <span class="c1"># numerical_eos)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_initial_guess</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Quadratic fit to get an initial guess for the parameters.</span>

<span class="sd">        Returns:</span>
<span class="sd">            tuple: (e0, b0, b1, v0)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyfit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">]</span>

        <span class="n">v0</span> <span class="o">=</span> <span class="o">-</span><span class="n">b</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">a</span><span class="p">)</span>
        <span class="n">e0</span> <span class="o">=</span> <span class="n">a</span> <span class="o">*</span> <span class="p">(</span><span class="n">v0</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">+</span> <span class="n">c</span>
        <span class="n">b0</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">a</span> <span class="o">*</span> <span class="n">v0</span>
        <span class="n">b1</span> <span class="o">=</span> <span class="mi">4</span>  <span class="c1"># b1 is usually a small number like 4</span>

        <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">vmin</span> <span class="o">&lt;</span> <span class="n">v0</span> <span class="ow">and</span> <span class="n">v0</span> <span class="o">&lt;</span> <span class="n">vmax</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">EOSError</span><span class="p">(</span><span class="s1">&#39;The minimum volume of a fitted parabola is &#39;</span>
                           <span class="s1">&#39;not in the input volumes</span><span class="se">\n</span><span class="s1">.&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span>

<div class="viewcode-block" id="EOSBase.fit"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSBase.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do the fitting. Does least square fitting. If you want to use custom</span>
<span class="sd">        fitting, must override this.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># the objective function that will be minimized in the least square</span>
        <span class="c1"># fitting</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_initial_guess</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span><span class="p">,</span> <span class="n">ierr</span> <span class="o">=</span> <span class="n">leastsq</span><span class="p">(</span><span class="k">lambda</span> <span class="n">pars</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">y</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_func</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">pars</span><span class="p">),</span>
                                        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">))</span>
        <span class="c1"># e0, b0, b1, v0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span>
        <span class="k">if</span> <span class="n">ierr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">EOSError</span><span class="p">(</span><span class="s2">&quot;Optimal parameters not found&quot;</span><span class="p">)</span></div>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The equation of state function. This must be implemented by all classes</span>
<span class="sd">        that derive from this abstract class.</span>

<span class="sd">        Args:</span>
<span class="sd">            volume (float/numpy.array)</span>
<span class="sd">             params (list/tuple): values for the parameters other than the</span>
<span class="sd">                volume used by the eos.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="EOSBase.func"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSBase.func">[docs]</a>    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The equation of state function with the paramters other than volume set</span>
<span class="sd">        to the ones obtained from fitting.</span>

<span class="sd">        Args:</span>
<span class="sd">             volume (list/numpy.array)</span>

<span class="sd">        Returns:</span>
<span class="sd">            numpy.array</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_func</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">volume</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            volume (): Volume</span>

<span class="sd">        Returns:</span>
<span class="sd">            Compute EOS with this volume.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">volume</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">e0</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the min energy.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">b0</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the bulk modulus.</span>
<span class="sd">        Note: the units for the bulk modulus: unit of energy/unit of volume^3.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">b0_GPa</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the bulk modulus in GPa.</span>
<span class="sd">        Note: This assumes that the energy and volumes are in eV and Ang^3</span>
<span class="sd">            respectively</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FloatWithUnit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">b0</span><span class="p">,</span> <span class="s2">&quot;eV ang^-3&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="s2">&quot;GPa&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">b1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the derivative of bulk modulus wrt pressure(dimensionless)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">v0</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the minimum or the reference volume in Ang^3.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">results</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a summary dict.</span>

<span class="sd">        Returns:</span>
<span class="sd">            dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">e0</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">b1</span><span class="p">,</span> <span class="n">v0</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">v0</span><span class="p">)</span>

<div class="viewcode-block" id="EOSBase.plot"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSBase.plot">[docs]</a>    <span class="k">def</span> <span class="nf">plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">plt</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot the equation of state.</span>

<span class="sd">        Args:</span>
<span class="sd">            width (float): Width of plot in inches. Defaults to 8in.</span>
<span class="sd">            height (float): Height of plot in inches. Defaults to width *</span>
<span class="sd">                golden ratio.</span>
<span class="sd">            plt (matplotlib.pyplot): If plt is supplied, changes will be made</span>
<span class="sd">                to an existing plot. Otherwise, a new plot will be created.</span>
<span class="sd">            dpi:</span>
<span class="sd">            kwargs (dict): additional args fed to pyplot.plot.</span>
<span class="sd">                supported keys: style, color, text, label</span>

<span class="sd">        Returns:</span>
<span class="sd">            Matplotlib plot object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span> <span class="o">=</span> <span class="n">pretty_plot</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="n">width</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="n">height</span><span class="p">,</span> <span class="n">plt</span><span class="o">=</span><span class="n">plt</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="n">dpi</span><span class="p">)</span>

        <span class="n">color</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;color&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
        <span class="n">label</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;label&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> fit&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Equation of State: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                 <span class="s2">&quot;Minimum energy = </span><span class="si">%1.2f</span><span class="s2"> eV&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">e0</span><span class="p">,</span>
                 <span class="s2">&quot;Minimum or reference volume = </span><span class="si">%1.2f</span><span class="s2"> Ang^3&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">v0</span><span class="p">,</span>
                 <span class="s2">&quot;Bulk modulus = </span><span class="si">%1.2f</span><span class="s2"> eV/Ang^3 = </span><span class="si">%1.2f</span><span class="s2"> GPa&quot;</span> <span class="o">%</span>
                 <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">b0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">b0_GPa</span><span class="p">),</span>
                 <span class="s2">&quot;Derivative of bulk modulus wrt pressure = </span><span class="si">%1.2f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">b1</span><span class="p">]</span>
        <span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>

        <span class="c1"># Plot input data.</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">&quot;None&quot;</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s2">&quot;o&quot;</span><span class="p">,</span>
                 <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">)</span>

        <span class="c1"># Plot eos fit.</span>
        <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">)</span>
        <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span> <span class="o">=</span> <span class="p">(</span><span class="n">vmin</span> <span class="o">-</span> <span class="mf">0.01</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vmin</span><span class="p">),</span> <span class="n">vmax</span> <span class="o">+</span> <span class="mf">0.01</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vmax</span><span class="p">))</span>
        <span class="n">vfit</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">vfit</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">vfit</span><span class="p">),</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">&quot;dashed&quot;</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span>
                 <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">&quot;Volume $</span><span class="se">\\</span><span class="s2">AA^3$&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">&quot;Energy (eV)&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s2">&quot;best&quot;</span><span class="p">,</span> <span class="n">shadow</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># Add text with fit parameters.</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">transform</span><span class="o">=</span><span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">transAxes</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">plt</span></div>

<div class="viewcode-block" id="EOSBase.plot_ax"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSBase.plot_ax">[docs]</a>    <span class="nd">@add_fig_kwargs</span>
    <span class="k">def</span> <span class="nf">plot_ax</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ax</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot the equation of state on axis `ax`</span>

<span class="sd">        Args:</span>
<span class="sd">            ax: matplotlib :class:`Axes` or None if a new figure should be created.</span>
<span class="sd">            fontsize: Legend fontsize.</span>
<span class="sd">            color (str): plot color.</span>
<span class="sd">            label (str): Plot label</span>
<span class="sd">            text (str): Legend text (options)</span>

<span class="sd">        Returns:</span>
<span class="sd">            Matplotlib figure object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ax</span><span class="p">,</span> <span class="n">fig</span><span class="p">,</span> <span class="n">plt</span> <span class="o">=</span> <span class="n">get_ax_fig_plt</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span>

        <span class="n">color</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;color&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
        <span class="n">label</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;label&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> fit&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Equation of State: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                 <span class="s2">&quot;Minimum energy = </span><span class="si">%1.2f</span><span class="s2"> eV&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">e0</span><span class="p">,</span>
                 <span class="s2">&quot;Minimum or reference volume = </span><span class="si">%1.2f</span><span class="s2"> Ang^3&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">v0</span><span class="p">,</span>
                 <span class="s2">&quot;Bulk modulus = </span><span class="si">%1.2f</span><span class="s2"> eV/Ang^3 = </span><span class="si">%1.2f</span><span class="s2"> GPa&quot;</span> <span class="o">%</span>
                 <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">b0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">b0_GPa</span><span class="p">),</span>
                 <span class="s2">&quot;Derivative of bulk modulus wrt pressure = </span><span class="si">%1.2f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">b1</span><span class="p">]</span>
        <span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>

        <span class="c1"># Plot input data.</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">&quot;None&quot;</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s2">&quot;o&quot;</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">)</span>

        <span class="c1"># Plot eos fit.</span>
        <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">)</span>
        <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span> <span class="o">=</span> <span class="p">(</span><span class="n">vmin</span> <span class="o">-</span> <span class="mf">0.01</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vmin</span><span class="p">),</span> <span class="n">vmax</span> <span class="o">+</span> <span class="mf">0.01</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vmax</span><span class="p">))</span>
        <span class="n">vfit</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>

        <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">vfit</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">vfit</span><span class="p">),</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">&quot;dashed&quot;</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>

        <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s2">&quot;Volume $</span><span class="se">\\</span><span class="s2">AA^3$&quot;</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s2">&quot;Energy (eV)&quot;</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s2">&quot;best&quot;</span><span class="p">,</span> <span class="n">shadow</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># Add text with fit parameters.</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="n">fontsize</span><span class="p">,</span> <span class="n">horizontalalignment</span><span class="o">=</span><span class="s1">&#39;center&#39;</span><span class="p">,</span>
                <span class="n">verticalalignment</span><span class="o">=</span><span class="s1">&#39;center&#39;</span><span class="p">,</span> <span class="n">transform</span><span class="o">=</span><span class="n">ax</span><span class="o">.</span><span class="n">transAxes</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">fig</span></div></div>


<div class="viewcode-block" id="Murnaghan"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.Murnaghan">[docs]</a><span class="k">class</span> <span class="nc">Murnaghan</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Murnaghan EOS.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        From PRB 28,5480 (1983)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">e0</span> <span class="o">+</span>
                <span class="n">b0</span> <span class="o">*</span> <span class="n">volume</span> <span class="o">/</span> <span class="n">b1</span> <span class="o">*</span> <span class="p">(((</span><span class="n">v0</span> <span class="o">/</span> <span class="n">volume</span><span class="p">)</span> <span class="o">**</span> <span class="n">b1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span>
                <span class="n">v0</span> <span class="o">*</span> <span class="n">b0</span> <span class="o">/</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">))</span></div>


<div class="viewcode-block" id="Birch"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.Birch">[docs]</a><span class="k">class</span> <span class="nc">Birch</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Birch EOS.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        From Intermetallic compounds: Principles and Practice, Vol. I:</span>
<span class="sd">        Principles Chapter 9 pages 195-210 by M. Mehl. B. Klein,</span>
<span class="sd">        D. Papaconstantopoulos.</span>
<span class="sd">        case where n=0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">e0</span>
                <span class="o">+</span> <span class="mf">9.0</span> <span class="o">/</span> <span class="mf">8.0</span> <span class="o">*</span> <span class="n">b0</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">*</span> <span class="p">((</span><span class="n">v0</span> <span class="o">/</span> <span class="n">volume</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
                <span class="o">+</span> <span class="mf">9.0</span> <span class="o">/</span> <span class="mf">16.0</span> <span class="o">*</span> <span class="n">b0</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">*</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mf">4.</span><span class="p">)</span> <span class="o">*</span>
                <span class="p">((</span><span class="n">v0</span> <span class="o">/</span> <span class="n">volume</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">**</span> <span class="mi">3</span><span class="p">)</span></div>


<div class="viewcode-block" id="BirchMurnaghan"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.BirchMurnaghan">[docs]</a><span class="k">class</span> <span class="nc">BirchMurnaghan</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    BirchMurnaghan EOS</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        BirchMurnaghan equation from PRB 70, 224107</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="n">eta</span> <span class="o">=</span> <span class="p">(</span><span class="n">v0</span> <span class="o">/</span> <span class="n">volume</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">e0</span> <span class="o">+</span>
                <span class="mf">9.</span> <span class="o">*</span> <span class="n">b0</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">/</span> <span class="mf">16.</span> <span class="o">*</span> <span class="p">(</span><span class="n">eta</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span>
                <span class="p">(</span><span class="mi">6</span> <span class="o">+</span> <span class="n">b1</span> <span class="o">*</span> <span class="p">(</span><span class="n">eta</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">-</span> <span class="mf">4.</span> <span class="o">*</span> <span class="n">eta</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span></div>


<div class="viewcode-block" id="PourierTarantola"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.PourierTarantola">[docs]</a><span class="k">class</span> <span class="nc">PourierTarantola</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    PourierTarantola EOS</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pourier-Tarantola equation from PRB 70, 224107</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="n">eta</span> <span class="o">=</span> <span class="p">(</span><span class="n">volume</span> <span class="o">/</span> <span class="n">v0</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="n">squiggle</span> <span class="o">=</span> <span class="o">-</span><span class="mf">3.</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">eta</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">e0</span> <span class="o">+</span> <span class="n">b0</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">*</span> <span class="n">squiggle</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">6.</span> <span class="o">*</span> <span class="p">(</span><span class="mf">3.</span> <span class="o">+</span> <span class="n">squiggle</span> <span class="o">*</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mi">2</span><span class="p">))</span></div>


<div class="viewcode-block" id="Vinet"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.Vinet">[docs]</a><span class="k">class</span> <span class="nc">Vinet</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Vinet EOS.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Vinet equation from PRB 70, 224107</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="n">eta</span> <span class="o">=</span> <span class="p">(</span><span class="n">volume</span> <span class="o">/</span> <span class="n">v0</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">e0</span> <span class="o">+</span> <span class="mf">2.</span> <span class="o">*</span> <span class="n">b0</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">/</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
                <span class="o">*</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">-</span> <span class="p">(</span><span class="mf">5.</span> <span class="o">+</span> <span class="mf">3.</span> <span class="o">*</span> <span class="n">b1</span> <span class="o">*</span> <span class="p">(</span><span class="n">eta</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">-</span> <span class="mf">3.</span> <span class="o">*</span> <span class="n">eta</span><span class="p">)</span>
                   <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">3.</span> <span class="o">*</span> <span class="p">(</span><span class="n">b1</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">eta</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)))</span></div>


<div class="viewcode-block" id="PolynomialEOS"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.PolynomialEOS">[docs]</a><span class="k">class</span> <span class="nc">PolynomialEOS</span><span class="p">(</span><span class="n">EOSBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Derives from EOSBase. Polynomial based equations of states must subclass</span>
<span class="sd">    this.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">params</span><span class="p">))(</span><span class="n">volume</span><span class="p">)</span>

<div class="viewcode-block" id="PolynomialEOS.fit"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.PolynomialEOS.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do polynomial fitting and set the parameters. Uses numpy polyfit.</span>

<span class="sd">        Args:</span>
<span class="sd">             order (int): order of the fit polynomial</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyfit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="n">order</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_params</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_set_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use the fit polynomial to compute the parameter e0, b0, b1 and v0</span>
<span class="sd">        and set to the _params attribute.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fit_poly</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span><span class="p">)</span>
        <span class="c1"># the volume at min energy, used as the intial guess for the</span>
        <span class="c1"># optimization wrt volume.</span>
        <span class="n">v_e_min</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">)]</span>
        <span class="c1"># evaluate e0, v0, b0 and b1</span>
        <span class="n">min_wrt_v</span> <span class="o">=</span> <span class="n">minimize</span><span class="p">(</span><span class="n">fit_poly</span><span class="p">,</span> <span class="n">v_e_min</span><span class="p">)</span>
        <span class="n">e0</span><span class="p">,</span> <span class="n">v0</span> <span class="o">=</span> <span class="n">min_wrt_v</span><span class="o">.</span><span class="n">fun</span><span class="p">,</span> <span class="n">min_wrt_v</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">pderiv2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">fit_poly</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">pderiv3</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">fit_poly</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
        <span class="n">b0</span> <span class="o">=</span> <span class="n">v0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">pderiv2</span><span class="p">)(</span><span class="n">v0</span><span class="p">)</span>
        <span class="n">db0dv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">pderiv2</span><span class="p">)(</span><span class="n">v0</span><span class="p">)</span> <span class="o">+</span> <span class="n">v0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">pderiv3</span><span class="p">)(</span><span class="n">v0</span><span class="p">)</span>
        <span class="c1"># db/dp</span>
        <span class="n">b1</span> <span class="o">=</span> <span class="o">-</span> <span class="n">v0</span> <span class="o">*</span> <span class="n">db0dv</span> <span class="o">/</span> <span class="n">b0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">e0</span><span class="p">,</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span><span class="p">]</span></div>


<div class="viewcode-block" id="DeltaFactor"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.DeltaFactor">[docs]</a><span class="k">class</span> <span class="nc">DeltaFactor</span><span class="p">(</span><span class="n">PolynomialEOS</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fitting a polynomial EOS using delta factor.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">volume</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">params</span><span class="p">))(</span><span class="n">x</span><span class="p">)</span>

<div class="viewcode-block" id="DeltaFactor.fit"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.DeltaFactor.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Overriden since this eos works with volume**(2/3) instead of volume.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">volumes</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyfit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="n">order</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_params</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_set_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Overriden to account for the fact the fit with volume**(2/3) instead</span>
<span class="sd">        of volume.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">deriv0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span><span class="p">)</span>
        <span class="n">deriv1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">deriv0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">deriv2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">deriv1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">deriv3</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">deriv2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">roots</span><span class="p">(</span><span class="n">deriv1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">deriv2</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">v0</span> <span class="o">=</span> <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">3.</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span>
                <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">EOSError</span><span class="p">(</span><span class="s2">&quot;No minimum could be found&quot;</span><span class="p">)</span>

        <span class="n">derivV2</span> <span class="o">=</span> <span class="mf">4.</span> <span class="o">/</span> <span class="mf">9.</span> <span class="o">*</span> <span class="n">x</span> <span class="o">**</span> <span class="mf">5.</span> <span class="o">*</span> <span class="n">deriv2</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="n">derivV3</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">20.</span> <span class="o">/</span> <span class="mf">9.</span> <span class="o">*</span> <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="mf">13.</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">*</span> <span class="n">deriv2</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mf">8.</span> <span class="o">/</span> <span class="mf">27.</span> <span class="o">*</span>
                   <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="mf">15.</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">*</span> <span class="n">deriv3</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
        <span class="n">b0</span> <span class="o">=</span> <span class="n">derivV2</span> <span class="o">/</span> <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="mf">3.</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span>
        <span class="n">b1</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">3.</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">*</span> <span class="n">derivV3</span> <span class="o">/</span> <span class="n">derivV2</span>

        <span class="c1"># e0, b0, b1, v0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">deriv0</span><span class="p">(</span><span class="n">v0</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)),</span> <span class="n">b0</span><span class="p">,</span> <span class="n">b1</span><span class="p">,</span> <span class="n">v0</span><span class="p">]</span></div>


<div class="viewcode-block" id="NumericalEOS"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.NumericalEOS">[docs]</a><span class="k">class</span> <span class="nc">NumericalEOS</span><span class="p">(</span><span class="n">PolynomialEOS</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A numerical EOS.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="NumericalEOS.fit"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.NumericalEOS.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">min_ndata_factor</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">max_poly_order_factor</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">min_poly_order</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fit the input data to the &#39;numerical eos&#39;, the equation of state employed</span>
<span class="sd">        in the quasiharmonic Debye model described in the paper:</span>
<span class="sd">        10.1103/PhysRevB.90.174107.</span>

<span class="sd">        credits: Cormac Toher</span>

<span class="sd">        Args:</span>
<span class="sd">            min_ndata_factor (int): parameter that controls the minimum number</span>
<span class="sd">                of data points that will be used for fitting.</span>
<span class="sd">                minimum number of data points =</span>
<span class="sd">                    total data points-2*min_ndata_factor</span>
<span class="sd">            max_poly_order_factor (int): parameter that limits the max order</span>
<span class="sd">                of the polynomial used for fitting.</span>
<span class="sd">                max_poly_order = number of data points used for fitting -</span>
<span class="sd">                                 max_poly_order_factor</span>
<span class="sd">            min_poly_order (int): minimum order of the polynomial to be</span>
<span class="sd">                considered for fitting.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;ignore&#39;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">RankWarning</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">get_rms</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">y</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>

        <span class="c1"># list of (energy, volume) tuples</span>
        <span class="n">e_v</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">energies</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">volumes</span><span class="p">)]</span>
        <span class="n">ndata</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">e_v</span><span class="p">)</span>
        <span class="c1"># minimum number of data points used for fitting</span>
        <span class="n">ndata_min</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ndata</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">min_ndata_factor</span><span class="p">,</span> <span class="n">min_poly_order</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">rms_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>
        <span class="c1"># number of data points available for fit in each iteration</span>
        <span class="n">ndata_fit</span> <span class="o">=</span> <span class="n">ndata</span>
        <span class="c1"># store the fit polynomial coefficients and the rms in a dict,</span>
        <span class="c1"># where the key=(polynomial order, number of data points used for</span>
        <span class="c1"># fitting)</span>
        <span class="n">all_coeffs</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># sort by energy</span>
        <span class="n">e_v</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">e_v</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="c1"># minimum energy tuple</span>
        <span class="n">e_min</span> <span class="o">=</span> <span class="n">e_v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="c1"># sort by volume</span>
        <span class="n">e_v</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">e_v</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="c1"># index of minimum energy tuple in the volume sorted list</span>
        <span class="n">emin_idx</span> <span class="o">=</span> <span class="n">e_v</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">e_min</span><span class="p">)</span>
        <span class="c1"># the volume lower than the volume corresponding to minimum energy</span>
        <span class="n">v_before</span> <span class="o">=</span> <span class="n">e_v</span><span class="p">[</span><span class="n">emin_idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="c1"># the volume higher than the volume corresponding to minimum energy</span>
        <span class="n">v_after</span> <span class="o">=</span> <span class="n">e_v</span><span class="p">[</span><span class="n">emin_idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">e_v_work</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">e_v</span><span class="p">)</span>

        <span class="c1"># loop over the data points.</span>
        <span class="k">while</span> <span class="p">(</span><span class="n">ndata_fit</span> <span class="o">&gt;=</span> <span class="n">ndata_min</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">e_min</span> <span class="ow">in</span> <span class="n">e_v_work</span><span class="p">):</span>
            <span class="n">max_poly_order</span> <span class="o">=</span> <span class="n">ndata_fit</span> <span class="o">-</span> <span class="n">max_poly_order_factor</span>
            <span class="n">e</span> <span class="o">=</span> <span class="p">[</span><span class="n">ei</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">ei</span> <span class="ow">in</span> <span class="n">e_v_work</span><span class="p">]</span>
            <span class="n">v</span> <span class="o">=</span> <span class="p">[</span><span class="n">ei</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">ei</span> <span class="ow">in</span> <span class="n">e_v_work</span><span class="p">]</span>
            <span class="c1"># loop over polynomial order</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">min_poly_order</span><span class="p">,</span> <span class="n">max_poly_order</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyfit</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
                <span class="n">pder</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyder</span><span class="p">(</span><span class="n">coeffs</span><span class="p">)</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">pder</span><span class="p">)(</span><span class="n">v_before</span><span class="p">)</span>
                <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">pder</span><span class="p">)(</span><span class="n">v_after</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">rms</span> <span class="o">=</span> <span class="n">get_rms</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">poly1d</span><span class="p">(</span><span class="n">coeffs</span><span class="p">)(</span><span class="n">v</span><span class="p">))</span>
                    <span class="n">rms_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">rms_min</span><span class="p">,</span> <span class="n">rms</span> <span class="o">*</span> <span class="n">i</span> <span class="o">/</span> <span class="n">ndata_fit</span><span class="p">)</span>
                    <span class="n">all_coeffs</span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">ndata_fit</span><span class="p">)]</span> <span class="o">=</span> <span class="p">[</span><span class="n">coeffs</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span> <span class="n">rms</span><span class="p">]</span>
                    <span class="c1"># store the fit coefficients small to large,</span>
                    <span class="c1"># i.e a0, a1, .. an</span>
                    <span class="n">all_coeffs</span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">ndata_fit</span><span class="p">)][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
            <span class="c1"># remove 1 data point from each end.</span>
            <span class="n">e_v_work</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">e_v_work</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">ndata_fit</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">e_v_work</span><span class="p">)</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;total number of polynomials: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">all_coeffs</span><span class="p">)))</span>

        <span class="n">norm</span> <span class="o">=</span> <span class="mf">0.</span>
        <span class="n">fit_poly_order</span> <span class="o">=</span> <span class="n">ndata</span>
        <span class="c1"># weight average polynomial coefficients.</span>
        <span class="n">weighted_avg_coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">fit_poly_order</span><span class="p">,))</span>

        <span class="c1"># combine all the filtered polynomial candidates to get the final fit.</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">all_coeffs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># weighted rms = rms * polynomial order / rms_min / ndata_fit</span>
            <span class="n">weighted_rms</span> <span class="o">=</span> <span class="n">v</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">rms_min</span> <span class="o">/</span> <span class="n">k</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">weight</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">weighted_rms</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span>
            <span class="n">norm</span> <span class="o">+=</span> <span class="n">weight</span>
            <span class="n">coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="c1"># pad the coefficient array with zeros</span>
            <span class="n">coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">coeffs</span><span class="p">,</span>
                                <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">fit_poly_order</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">coeffs</span><span class="p">),</span> <span class="mi">0</span><span class="p">)),</span>
                                <span class="s1">&#39;constant&#39;</span><span class="p">)</span>
            <span class="n">weighted_avg_coeffs</span> <span class="o">+=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">coeffs</span>

        <span class="c1"># normalization</span>
        <span class="n">weighted_avg_coeffs</span> <span class="o">/=</span> <span class="n">norm</span>
        <span class="n">weighted_avg_coeffs</span> <span class="o">=</span> <span class="n">weighted_avg_coeffs</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="c1"># large to small(an, an-1, ..., a1, a0) as expected by np.poly1d</span>
        <span class="n">weighted_avg_coeffs</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">eos_params</span> <span class="o">=</span> <span class="n">weighted_avg_coeffs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_params</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="EOS"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOS">[docs]</a><span class="k">class</span> <span class="nc">EOS</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convenient wrapper. Retained in its original state to ensure backward</span>
<span class="sd">    compatibility.</span>

<span class="sd">    Fit equation of state for bulk systems.</span>

<span class="sd">    The following equations are supported::</span>

<span class="sd">        murnaghan: PRB 28, 5480 (1983)</span>

<span class="sd">        birch: Intermetallic compounds: Principles and Practice, Vol I:</span>
<span class="sd">            Principles. pages 195-210</span>

<span class="sd">        birch_murnaghan: PRB 70, 224107</span>

<span class="sd">        pourier_tarantola: PRB 70, 224107</span>

<span class="sd">        vinet: PRB 70, 224107</span>

<span class="sd">        deltafactor</span>

<span class="sd">        numerical_eos: 10.1103/PhysRevB.90.174107.</span>

<span class="sd">    Usage::</span>

<span class="sd">       eos = EOS(eos_name=&#39;murnaghan&#39;)</span>
<span class="sd">       eos_fit = eos.fit(volumes, energies)</span>
<span class="sd">       eos_fit.plot()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">MODELS</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;murnaghan&quot;</span><span class="p">:</span> <span class="n">Murnaghan</span><span class="p">,</span>
        <span class="s2">&quot;birch&quot;</span><span class="p">:</span> <span class="n">Birch</span><span class="p">,</span>
        <span class="s2">&quot;birch_murnaghan&quot;</span><span class="p">:</span> <span class="n">BirchMurnaghan</span><span class="p">,</span>
        <span class="s2">&quot;pourier_tarantola&quot;</span><span class="p">:</span> <span class="n">PourierTarantola</span><span class="p">,</span>
        <span class="s2">&quot;vinet&quot;</span><span class="p">:</span> <span class="n">Vinet</span><span class="p">,</span>
        <span class="s2">&quot;deltafactor&quot;</span><span class="p">:</span> <span class="n">DeltaFactor</span><span class="p">,</span>
        <span class="s2">&quot;numerical_eos&quot;</span><span class="p">:</span> <span class="n">NumericalEOS</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">eos_name</span><span class="o">=</span><span class="s1">&#39;murnaghan&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            eos_name (str): Type of EOS to fit.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">eos_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">MODELS</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">EOSError</span><span class="p">(</span><span class="s2">&quot;The equation of state &#39;</span><span class="si">{}</span><span class="s2">&#39; is not supported. &quot;</span>
                           <span class="s2">&quot;Please choose one from the following list: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span>
                           <span class="nb">format</span><span class="p">(</span><span class="n">eos_name</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">MODELS</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_eos_name</span> <span class="o">=</span> <span class="n">eos_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">MODELS</span><span class="p">[</span><span class="n">eos_name</span><span class="p">]</span>

<div class="viewcode-block" id="EOS.fit"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOS.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volumes</span><span class="p">,</span> <span class="n">energies</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fit energies as function of volumes.</span>

<span class="sd">        Args:</span>
<span class="sd">            volumes (list/np.array)</span>
<span class="sd">            energies (list/np.array)</span>

<span class="sd">        Returns:</span>
<span class="sd">            EOSBase: EOSBase object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">eos_fit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">volumes</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">energies</span><span class="p">))</span>
        <span class="n">eos_fit</span><span class="o">.</span><span class="n">fit</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">eos_fit</span></div></div>


<div class="viewcode-block" id="EOSError"><a class="viewcode-back" href="../../../pymatgen.analysis.eos.html#pymatgen.analysis.eos.EOSError">[docs]</a><span class="k">class</span> <span class="nc">EOSError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Error class for EOS fitting.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.eos</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>